package com.beta.auth.config;

import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.WxMpHostConfig;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author gwqin
 * @description 公众号授权相关配置
 * @date 2023-07-10 11:34
 */
@Configuration
public class WeChatMpConfig {

    @Autowired
    private UserAuthCfgBean userAuthCfgBean;

    @Bean
    public WxMpService wxMpService() {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }

    private WxMpConfigStorage wxMpConfigStorage() {
        WxMpDefaultConfigImpl wxMpConfigStorage = new WxMpDefaultConfigImpl();
        wxMpConfigStorage.setAppId(userAuthCfgBean.getMpAppId());
        wxMpConfigStorage.setSecret(userAuthCfgBean.getMpAppSecret());

        String apiHost = userAuthCfgBean.getApiHost();
//        String mpHost = userAuthCfgBean.getMpHost();
//        String openHost = userAuthCfgBean.getOpenHost();

        if (StringUtils.isNotEmpty(apiHost)) {
            WxMpHostConfig wxMpHostConfig = new WxMpHostConfig();

            if (StringUtils.isNotEmpty(apiHost)) {
                wxMpHostConfig.setApiHost(apiHost);
            }
//            if (StringUtils.isNotEmpty(mpHost)) {
//                wxMpHostConfig.setMpHost(mpHost);
//            }
//
//            if (StringUtils.isNotEmpty(openHost)) {
//                wxMpHostConfig.setOpenHost(openHost);
//            }

            wxMpConfigStorage.setHostConfig(wxMpHostConfig);
        }

        return wxMpConfigStorage;
    }
}